<?php

declare(strict_types=1);

namespace app\api\listener;

use app\common\service\sys\UserService;
use app\common\service\sys\LogService;

/**
 * 用户登录事件
 */
class UserLogin
{
  /**
   * 事件监听处理
   * @return mixed
   */
  public function handle($event)
  {
    list($user, $node, $time, $ip, $ln) = $event;
    $data = [
      'last_time' => $time,
      'last_ip' => $ip,
      'ln' => $ln + 1,
      'online' => 1
    ];
    (new UserService())->update($user['id'], $data);
    (new LogService())->add([
      'uid' => $user['id'],
      'un' => $user['un'],
      'node' => $node,
      'create_time' => $time,
      'ip' => $ip,
      'action' => 'api登录',
      'content'  => $user['real_name'],
    ]);
  }
}
